//
//  UITableView+Extension.swift
//  NewTradmarkSerice
//
//  Created by hlt on 2023/4/4.
//

import UIKit
extension UITableView{
    convenience init(delegate:Any, style:UITableView.Style = .plain) {
         self.init(frame: .zero, style: style)
        //UITableView(frame: .zero, style: style)
        if #available(iOS 15.0, *) {
            self.isPrefetchingEnabled = false
        }
        if let delegate = delegate as? UITableViewDelegate{
            self.delegate = delegate
        }
        if let datasource = delegate as? UITableViewDataSource{
            self.dataSource = datasource
        }
       
        let footView = UIView()
        self.estimatedRowHeight = 0
        self.tableFooterView = footView
        self.contentInsetAdjustmentBehavior = .never
        if style == .grouped{
            if #available(iOS 15.0, *) {
                self.sectionHeaderTopPadding = 0.0
                self.isPrefetchingEnabled = false
            }
            self.estimatedSectionFooterHeight = 0.0
            self.estimatedSectionHeaderHeight = 0.0
        }
        
    }
    
    func registerCells(cellClasses:[UITableViewCell.Type]){
        cellClasses.forEach { cellType in
            self.register(cellType)
        }
    }
    
    
    func dequeueReusableCell<T:UITableViewCell>(withCellClass cellClass:T.Type, for indexPath: IndexPath) -> T{
        let cell = self.dequeueReusableCell(withIdentifier: NSStringFromClass(cellClass), for: indexPath) as! T
        cell.selectionStyle = .none
        return cell
    }
    
    func register<T:UITableViewCell>(_ cellClass: T.Type){
        self.register(cellClass, forCellReuseIdentifier: NSStringFromClass(cellClass))
    }
}



